package com.study.rabbitmq.controller;

import com.study.rabbitmq.mqtt.MqttConsumerConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/consumer")
public class MessageReceiveController {

    @Autowired
    private MqttConsumerConfig mqttConsumerConfig;

    @GetMapping("/connect")
    public String connect() {
        log.info("MessageReceiveController connect");
        try {
            mqttConsumerConfig.connect();
            return mqttConsumerConfig.getMqttConfig().getClientId() + "连接到服务器";
        } catch (Exception e) {
            e.printStackTrace();
            log.error("MessageReceiveController connect 发生异常：{}", e.getMessage());
            return "connect发生异常";
        }
    }

    @GetMapping("/disConnect")
    public String disConnect() {
        log.info("MessageReceiveController disConnect");
        try {
            mqttConsumerConfig.disConnect();
            return mqttConsumerConfig.getMqttConfig().getClientId() + "与服务器断开连接";
        } catch (Exception e) {
            e.printStackTrace();
            log.error("MessageReceiveController disConnect 发生异常：", e.getMessage());
            return "disConnect发生异常";
        }
    }

}
